﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using ScrumTable.Common;
using ScrumTable.DL.Data.Common.Properties;

#endregion

namespace ScrumTable.DL.Data.Common
{
    /// <summary>
    /// This class represents a marker attribute for all plug in assemblies.
    /// It defines the entry point (IDataContext) of the plug in.
    /// </summary>
    [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false, Inherited = false)]
    public class DataAssemblyAttribute : Attribute
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly Type _dataAssemblyDriverActivatorType;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Returns the main entry type of the data assembly. A data assembly
        /// (ScrumTable PlugIn) is also known as data driver.
        /// </summary>
        public Type DataAssemblyDriverActivatorType
        {
            get { return _dataAssemblyDriverActivatorType; }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new data assembly attribute with the given entry point
        /// type.
        /// </summary>
        /// <param name="dataAssemblyDriverActivatorType">Specifies the main entry type of the data assembly. A data assembly (ScrumTable PlugIn) is also known as data driver.</param>
        public DataAssemblyAttribute(Type dataAssemblyDriverActivatorType)
        {
            PreCondition.AssertNotNull(dataAssemblyDriverActivatorType, "dataAssemblyDriverActivatorType");

            _dataAssemblyDriverActivatorType = dataAssemblyDriverActivatorType;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new data schema source selector.
        /// </summary>
        /// <returns>Returns the obtained selector instance.</returns>
        public IDataAssemblyDriverActivator CreateDataAssemblyDriverActivator()
        {
            if (_dataAssemblyDriverActivatorType == null)
            {
                throw new InvalidOperationException(Resources.ExcDataDriverDataTypeNull);
            }
            if (!typeof(IDataAssemblyDriverActivator).IsAssignableFrom(_dataAssemblyDriverActivatorType))
            {
                throw new InvalidOperationException(Resources.ExcDataDriverInvalidClass);
            }
            return (IDataAssemblyDriverActivator)Activator.CreateInstance(_dataAssemblyDriverActivatorType);
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}